package com.hrms.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hrms.pojo.Admin;
import com.hrms.service.AdminService;

@Controller
@RequestMapping("/admin")
public class AdminController {

	@Resource
	private AdminService adminService;

	// 添加管理员
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(HttpServletRequest req) {
		String adminName = req.getParameter("adminName");
		Admin admin = adminService.getByName(adminName);
		if (admin != null) {
			return "admin/add_fail";
		}
		String password = req.getParameter("password");
		try {
			adminService.addAdminL0(adminName, password);
		} catch (Exception e) {
			return "forward:/error/error";
		}
		return "admin/add_succ";
	}

	// 添加管理员页面跳转
	@RequestMapping("/add_admin")
	public String add_admin() {
		return "admin/add_admin";
	}

	// 根据id删除管理员
	@RequestMapping("/{adminId}/remove")
	public String remove(@PathVariable("adminId") Integer adminId) {
		Admin admin = adminService.getById(adminId);
		if (admin == null) {
			return "forward:/error/error";
		}
		try {
			adminService.removeById(adminId);
		} catch (Exception e) {
			return "forward:/error/error";
		}
		return "admin/rem_succ";
	}

	// 查看所有管理员
	@RequestMapping("/list_admin")
	public String listAdmin(Model model) {
		List<Admin> adminList = adminService.getAll();
		model.addAttribute("adminList", adminList);
		return "admin/list_admin";
	}

	// 系统管理员界面入口跳转
	@RequestMapping("/admin_L1")
	public String adminL1() {
		return "admin/admin_L1";
	}

	// 修改密码页面跳转
	@RequestMapping("/alt_pass")
	public String altPass() {
		return "admin/alt_pass";
	}

	// 修改密码
	@RequestMapping(value = "/{adminId}/alter_password")
	public String alterPassword(@PathVariable("adminId") Integer adminId, HttpServletRequest req) {
		Object obj = req.getSession().getAttribute("admin");
		if (obj == null) {
			return "forward:/index.jsp";
		}
		if (adminId == null) {
			return "forward:/error/error";
		}
		Admin admin = adminService.getById(adminId);
		if (admin == null) {
			return "forward:/error/error";
		}
		String adminName = admin.getAdminName();
		String oldPassword = req.getParameter("oldPassword");
		Admin ad = adminService.isLogin(adminName, oldPassword);
		if (ad == null) {
			return "admin/alt_fail";
		}
		String password = req.getParameter("passwrod");
		admin.setPassword(password);
		try {
			adminService.alterPassword(admin);
		} catch (Exception e) {
			return "forward:/error/error";
		}
		return "admin/alt_succ";

	}

	// 登录
	@RequestMapping(value = "/login")
	public String isLogin(HttpServletRequest req) {
		String adminName = req.getParameter("adminName");
		String password = req.getParameter("password");
		Admin admin = adminService.isLogin(adminName, password);
		if (admin == null) {
			// 登录失败
			return "errorLogin";
		}
		req.getSession().setAttribute("admin", admin);
		return "function";
	}

	// 功能界面跳转
	@RequestMapping("/function")
	public String function() {
		return "function";
	}

	// 登出
	@RequestMapping("/loginout")
	public String loginOut(HttpServletRequest req) {
		req.getSession().invalidate();
		return "forward:/index.jsp";
	}
}
